package com.loie.datacenter;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.loie.datacenter.model.*;
import com.loie.datacenter.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
import java.util.*;


/**
 * @author
 * @since 2018/12/13
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DataCenterApplication.class)

@Slf4j
@MapperScan("com.atguigu.eduservice.mapper")
public
class module_copydata {
    //@Autowired
    //TbDcUserMapper tbDcUserMapper;
    @Autowired
    private
    ShipmentService shipmentService;
    @Autowired
    private
    ShipmentServiceBackup shipmentServiceBackup;

    @Autowired
    private
    ShipmentsLineService shipmentsLineService;

    @Autowired
    private
    ShipmentsLineServiceBackup shipmentsLineServiceBackup;


    @Autowired
    private
    ShipmentsDeliveryServiceBackup shipmentsDeliveryServiceBackup;

    @Autowired
    private
    ShipmentsDeliveryService shipmentsDeliveryService;


    @Autowired
    private
    ShipmentsReportService shipmentsReportService;

    @Autowired
    private
    ShipmentsReportServiceBackup shipmentsReportServiceBackup;

    @Autowired
    private SalesOrdersService salesOrdersService;

    @Autowired
    private SalesOrdersServiceBackup salesOrdersServiceBackup;

    @Autowired
    private SalesOrderslineService salesOrderslineService;

    @Autowired
    private SalesOrderslineServiceBackup salesOrderslineServiceBackup;

    @Autowired
    private SalesOrdersDeliveryService salesOrdersDeliveryService;

    @Autowired
    private SalesOrdersDeliveryServiceBackup salesOrdersDeliveryServiceBackup;

    @Autowired
    private YhbgService yhbgService;

    @Autowired
    private YhbgServiceBackup yhbgServiceBackup;












@Test
public void yhgb_copyfromqc(){
    String purchaseOrderNo="C68LF2156-3";
    QueryWrapper query = new QueryWrapper();
    QueryWrapper querysub = new QueryWrapper();
    query.eq("PurchaseOrderNo",purchaseOrderNo);

    List<Yhbg> yhgbList=yhbgService.list(query);
    if (yhgbList.size()>0) {
        List<Yhbg> yhbgListbakcup = yhbgServiceBackup.list(query);
        if (yhbgListbakcup.size() > 0) {
            yhbgService.remove(query);
            log.info(String.format("删除验货报告：采购合同号:%d的数据", purchaseOrderNo));

        }
        for (Yhbg yhbg : yhgbList) {


            yhbg.setLastModified(new Date());
            yhbg.setRecordID(UUID.randomUUID().toString().toUpperCase());
            //querysub= new QueryWrapper();
            //querysub.apply("salesorderno in(select recordid from shipments where invoiceno={0})",shipments.getInvoiceNO());

        }
        yhbgServiceBackup.saveBatch(yhgbList);
    }
}


    @Test public
    void salesorders_copyfromqc() {
        QueryWrapper query = new QueryWrapper();
        QueryWrapper querysub = new QueryWrapper();
        //query.select("top 10 *");
//        query.gt("shippingdate","2023-01-01");
//        query.le("shippingdate","2023-01-31");
        query.eq("invoiceno","C66DS23008");
        List<Shipments> shipmentsList=shipmentService.list(query);
        for (int i=0;i<=shipmentsList.size()-1;i++){
            Shipments shipments=shipmentsList.get(i);
            querysub= new QueryWrapper();
            querysub.eq("parentid",shipments.getRecordID());
            List<ShipmentsLine> shipmentsLines=shipmentsLineService.list(querysub);
            List<ShipmentsDelivery> shipmentsDeliveryList=shipmentsDeliveryService.list(querysub);
            List<ShipmentsReport> shipmentsReportList=shipmentsReportService.list(querysub);
            //querysub= new QueryWrapper();
            //querysub.apply("salesorderno in(select recordid from shipments where invoiceno={0})",shipments.getInvoiceNO());
            //log.info("复制外销合同:"+salesOrders.getSalesOrderNo()+"订单产品卡");
            List<SalesOrders> salesOrdersList = salesOrdersService.list(new QueryWrapper<SalesOrders>()
                    //.select(Negotiation.class, info -> !excludeFields.contains(info.getProperty()))
                    .inSql("salesorderno", "select distinct shipmentsline.salesorderno from shipmentsline inner join   shipments on shipments.recordid=shipmentsline.parentid where shipments.recordid ='" + shipments.getRecordID() + "'")
                    );
            List<SalesOrdersline> salesOrderslineList = salesOrderslineService.list(new QueryWrapper<SalesOrdersline>()
                    //.select(Negotiation.class, info -> !excludeFields.contains(info.getProperty()))
                    .inSql("parentid", "select salesorders.recordid from salesorders inner join  shipmentsline on shipmentsline.salesorderno=salesorders.salesorderno inner join   shipments on shipments.recordid=shipmentsline.parentid where shipments.recordid ='" + shipments.getRecordID() + "'")
            );
            List<SalesOrdersDelivery> salesOrdersDeliveryList = salesOrdersDeliveryService.list(new QueryWrapper<SalesOrdersDelivery>()
                    //.select(Negotiation.class, info -> !excludeFields.contains(info.getProperty()))
                    .inSql("parentid", "select salesorders.recordid from salesorders inner join  shipmentsline on shipmentsline.salesorderno=salesorders.salesorderno inner join   shipments on shipments.recordid=shipmentsline.parentid where shipments.recordid ='" + shipments.getRecordID() + "'")
            );



            querysub= new QueryWrapper();
            querysub.eq("invoiceno",shipments.getInvoiceNO());


            Integer count=shipmentServiceBackup.count(querysub);
            if (count==0){
                //querysub= new QueryWrapper();



            }
            else
            {
                querysub= new QueryWrapper();
                querysub.apply("parentid in(select recordid from shipments where invoiceno={0})",shipments.getInvoiceNO());
                shipmentsDeliveryServiceBackup.remove(querysub);
                shipmentsReportServiceBackup.remove(querysub);
                shipmentsReportServiceBackup.remove(querysub);
                querysub= new QueryWrapper();
                querysub.apply("parentid in(select salesorders.recordid from salesorders inner join  shipmentsline on shipmentsline.salesorderno=salesorders.salesorderno inner join   shipments on shipments.recordid=shipmentsline.parentid where shipments.recordid={0})",shipments.getRecordID());
                salesOrderslineServiceBackup.remove(querysub);
                salesOrdersDeliveryServiceBackup.remove(querysub);
                querysub= new QueryWrapper();
                querysub.apply("SalesOrderNo in(select distinct shipmentsline.salesorderno from shipmentsline inner join   shipments on shipments.recordid=shipmentsline.parentid where shipments.recordid={0})",shipments.getRecordID());
                salesOrdersServiceBackup.remove(querysub);




                querysub= new QueryWrapper();
                querysub.eq("invoiceno",shipments.getInvoiceNO());
                shipmentServiceBackup.remove(querysub);



            }
            shipmentsLineServiceBackup.saveBatch(shipmentsLines);
            shipmentsDeliveryServiceBackup.saveBatch(shipmentsDeliveryList);
            shipmentsReportServiceBackup.saveBatch(shipmentsReportList);
            shipmentServiceBackup.save(shipments);
            salesOrdersServiceBackup.saveBatch(salesOrdersList);
            salesOrderslineServiceBackup.saveBatch(salesOrderslineList);
            salesOrdersDeliveryServiceBackup.saveBatch(salesOrdersDeliveryList);


            log.info(String.format("第%d/共%d条-发票号码：%s保存到备份数据库",
                    i+1,shipmentsList.size(),shipments.getInvoiceNO()));
        }
       // System.out.println(shipmentsList);





    }


//    @Test
//    public
//    void salesorders_scopy() {
//        QueryWrapper<SalesOrders> wrapper = new QueryWrapper<>();
//        wrapper.eq("salesorderno", "A79AF1512")
//                .orderByAsc("created");
//        List<SalesOrders> salesOrdersList = qcSalesOrdersService.list(wrapper);
//        UpdateWrapper updatesub = new UpdateWrapper<>();
//        QueryWrapper query = new QueryWrapper();
//        for (SalesOrders salesOrders : salesOrdersList) {
//            wrapper = new QueryWrapper<>();
//            wrapper.eq("salesorderno", salesOrders.getSalesOrderNo());
//            int count = zhSalesOrdersService.count(wrapper);
////            if (count>0) {
////                UpdateWrapper maindelrapper=new UpdateWrapper<>();
////                maindelrapper.eq("parentid",salesOrders.getRecordID());
////                zhSalesOrderslineService.remove(maindelrapper);
////                zhSalesOrdersDeliveryService.remove(maindelrapper);
////                maindelrapper=new UpdateWrapper<>();
////                maindelrapper.eq("recordid",salesOrders.getRecordID());
////                zhSalesOrdersService.remove(maindelrapper);
////
////
////            }
//            if (count == 0) {
//                zhSalesOrdersService.save(salesOrders);
//
//                QueryWrapper<SalesOrdersline> subline = new QueryWrapper<>();
//                subline.eq("parentid", salesOrders.getRecordID());
//
//
//                List<SalesOrdersline> salesOrderslines = qcSalesOrderslineService.list(subline);
//                if (salesOrderslines.size() > 0)
//                    zhSalesOrderslineService.saveBatch(salesOrderslines);
//
//                QueryWrapper<SalesOrdersDelivery> subdeliver = new QueryWrapper<>();
//                subdeliver.eq("parentid", salesOrders.getRecordID());
//                List<SalesOrdersDelivery> salesOrdersDeliveryList = qcSalesOrdersDeliveryService.list(subdeliver);
//                if (salesOrderslines.size() > 0)
//                    zhSalesOrdersDeliveryService.saveBatch(salesOrdersDeliveryList);
//            }
//            //复制客户信息
//            List<String> excludeFields = Arrays.asList("cpyj", "cpyj1", "cpyj2", "cplx","gsxyzed","sxedUsed","sxedRemain",
//            "keyAccountManager","salesRap","tcbl","djzed","djUsed","djRemin");
//
//            query = new QueryWrapper();
//            //query.select(Customers.class, info -> !excludeFields.contains(info.getProperty()))
//           // query.eq("customerno", salesOrders.getCustomerNo());
//
//                Customers customers = qcCustomerService.getOne(new QueryWrapper<Customers>()
//                        .select(Customers.class, info -> !excludeFields.contains(info.getProperty()))
//                        .eq("customerno", salesOrders.getCustomerNo())
//
//
//                );
//            //System.out.println(customers);
//            if (!Strings.isEmpty(customers.getCustomerNo())&& !customers.getCustomerNo().isEmpty()){
//                zhCustomerService.save(customers);
//                query = new QueryWrapper();
//                query.eq("parentid", customers.getRecordID());
//                List<CustomerBank> customerBankList = qcCustomerBankService.list(query);
//                if (customerBankList.size() > 0)
//                    zhCustomerBankService.saveBatch(customerBankList);
//                List<CustomerContacts> customerContactsList = qcCustomerContactsService.list(query);
//                if (customerContactsList.size() > 0)
//                    zhCustomerContactsService.saveBatch(customerContactsList);
//
//            }
//            //复制客户产品
//
//
//
//            List<CustomerItems> customerItemsList = qcCustomerItemsService.list(new QueryWrapper<CustomerItems>()
//                    .select(CustomerItems.class, info -> !excludeFields.contains(info.getProperty()))
//                    .inSql("CustomerItemNO", "select CustomerItemNO from SalesOrdersline inner join   salesorders on salesorders.recordid=SalesOrdersline.parentid where salesorders.recordid ='" + salesOrders.getRecordID() + "'")
//
//                    .eq("customerno", salesOrders.getCustomerNo()));
//            // System.out.println(customerItemsList);
//            //客户产品查询
//            for (CustomerItems customerItems : customerItemsList) {
//
//                count = zhCustomerItemsService.count(new QueryWrapper<CustomerItems>()
//                        .eq("CustomerItemNO", customerItems.getCustomerItemNO())
//                        .eq("customerno", customerItems.getCustomerNo()));
//                if (count == 0) {
//                    zhCustomerItemsService.save(customerItems);
//                    query = new QueryWrapper();
//
//                    query.eq("parentid", customerItems.getRecordID());
//                    List<ItemsQuote1> itemsQuote1List = qcCusotmerItemsQuoteService.list(query);
//                    if (itemsQuote1List.size() > 0)
//                        zhCusotmerItemsQuoteService.saveBatch(itemsQuote1List);
//                    List<ItemsConstruction1> itemsConstruction1List = qcItemsConstruction1Service.list(query);
//                    if (itemsConstruction1List.size() > 0)
//                        zhItemsConstruction1Service.saveBatch(itemsConstruction1List);
//                }
//            }
//                //复制产品信息
//
//                //List<String> excludeFields = Arrays.asList("cpyj", "cpyj1", "cpyj2", "cplx");
//
//
//                List<Items> zhItemsList = qcItemsService.list(new QueryWrapper<Items>()
//                        .select(ZhItems.class, info -> !excludeFields.contains(info.getProperty()))
//                        .inSql("ItemNO", "select ItemNO from SalesOrdersline inner join   salesorders on salesorders.recordid=SalesOrdersline.parentid where salesorders.recordid ='" + salesOrders.getRecordID() + "'"));
//
//
//                // System.out.println(customerItemsList);
//                //客户产品查询
//                for (ZhItems zhItems : zhItemsList) {
//
//                    count = zhItemsService.count(new QueryWrapper<ZhItems>()
//                            .eq("ItemNO", zhItems.getItemNo()));
//
//                    if (count == 0) {
//                        zhItemsService.save(zhItems);
//                        query = new QueryWrapper();
//
//                        query.eq("parentid", zhItems.getRecordID());
//                        List<ZhItemsQuote> itemsQuote1List = qcItemsQuoteService.list(query);
//                        if (itemsQuote1List.size() > 0)
//                            zhItemsQuoteService.saveBatch(itemsQuote1List);
//                        List<ZhItemsConstruction> itemsConstruction1List = qcItemsConstructionService.list(query);
//                        if (itemsConstruction1List.size() > 0)
//                            zhItemsConstructionService.saveBatch(itemsConstruction1List);
//                    }
//                }
//                    //***********复制工厂信息************
//                    List<Suppliers> suppliersList = qcSuppliersService.list(new QueryWrapper<Suppliers>()
//                            .select(Suppliers.class, info -> !excludeFields.contains(info.getProperty()))
//                            .inSql("supplierno", "select supplierno from SalesOrdersline inner join   salesorders on salesorders.recordid=SalesOrdersline.parentid where salesorders.recordid ='" + salesOrders.getRecordID() + "'"));
//
//
//                    for (Suppliers suppliers : suppliersList) {
//
//                        count = zhSuppliersService.count(new QueryWrapper<Suppliers>()
//                                .eq("supplierno", suppliers.getSupplierNo()));
//
//                        if (count == 0) {
//                            zhSuppliersService.save(suppliers);
//                            query = new QueryWrapper();
//
//                            query.eq("parentid", suppliers.getRecordID());
//                            List<SupplierContacts> supplierContactsList = qcSupplierContactsService.list(query);
//                            if (supplierContactsList.size() > 0)
//                                zhSupplierContactsService.saveBatch(supplierContactsList);
//                            List<BankInfo> bankInfoList = qcSupplierBankInfoService.list(query);
//                            if (bankInfoList.size() > 0)
//                                zhSupplierBankInfoService.saveBatch(bankInfoList);
//                        }
//
//
//                    }
//
//                    System.out.println(salesOrders.getSalesOrderNo() + "已保存");
//
//                    System.out.println(salesOrders.getSalesOrderNo() + "已处理");
//                }
//                //System.out.println(salesOrdersList);
//
//            }
        }



